/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-04-22     01467899       the first version
 */
/*Includes**********************************************************************/
#include "UsartDemo.h"
#include "string.h"
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;  /* 初始化配置参数 */

/* 用于接收消息的信号量 */
struct rt_semaphore rx_sem;
static rt_device_t serial;

/**
  * @brief  uart_input //接收数据回调函数
  * @param  dev
  *         size
  * @retval RT_EOK
  */
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
    /* 串口接收到数据后产生中断，调用此回调函数，然后发送接收信号量 */
    rt_sem_release(&rx_sem);

    return RT_EOK;
}
#if 0
/**
  * @brief  serial_thread_entry
  * @param  parameter
  * @retval None
  */
static void serial_thread_entry(void *parameter)
{
    char ch;

    while (1)
    {
        /* 从串口读取一个字节的数据，没有读取到则等待接收信号量 */
        while (rt_device_read(serial, -1, &ch, 1) != 1)
        {
            rt_device_write(serial, 0, &ch, 1);
            /* 阻塞等待接收信号量，等到信号量后再次读取数据 */
            rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
        }
        /* 读取到的数据输出 */
        rt_kprintf("%c",ch);
    }
}
#endif
#if 0
/*
********************************************************************************
 *                                          线程函数 -串口接收不定长数据
********************************************************************************
 */
#define SAMPLE_UART_NAME              "uart1"
#define DATA_CMD_END                   '\r'       /* 结束位设置为 \r，即回车符 */
#define ONE_DATA_MAXLEN                 20        /* 不定长数据的最大长度 */
static void serial_thread_entry(void *parameter)
{
    char ch;
    char data[ONE_DATA_MAXLEN];
    static char i = 0;

    while (1)
    {
        while (rt_device_read(serial, 0, &ch, 1) == 0)
        {
            rt_sem_control(&rx_sem, RT_IPC_CMD_RESET, RT_NULL);
            rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
        }
        rt_device_write(serial, 0, &ch, 1);
        if(ch == DATA_CMD_END)
        {
            data[i++] = '\0';
            rt_kprintf("data=%s\r\n",data);
            i = 0;
            continue;
        }
        i = (i >= ONE_DATA_MAXLEN-1) ? ONE_DATA_MAXLEN-1 : i;
        data[i++] = ch;
    }
}
#endif
static char uart_sample_get_char(void)
{
    char ch;

    while (rt_device_read(serial, 0, &ch, 1) == 0)
    {
        rt_sem_control(&rx_sem, RT_IPC_CMD_RESET, RT_NULL);
        rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
    }
    return ch;
}
static char Int2Char(int8_t Data)
{
    char Temp_Change = 0;
    if(Data == 0)
    {
        Temp_Change = '0';
    }
    else if(Data == 1)
    {
        Temp_Change = '1';
    }
    else if(Data == 2)
    {
        Temp_Change = '2';
    }
    else if(Data == 3)
    {
        Temp_Change = '3';
    }
    else if(Data == 4)
    {
        Temp_Change = '4';
    }
    else if(Data == 5)
    {
        Temp_Change = '5';
    }
    else if(Data == 6)
    {
        Temp_Change = '6';
    }
    else if(Data == 7)
    {
        Temp_Change = '7';
    }
    else if(Data == 8)
    {
        Temp_Change = '8';
    }
    else if(Data == 9)
    {
        Temp_Change = '9';
    }
    else
    {
        Temp_Change = ' ';
    }
    return Temp_Change;
}
extern rt_uint32_t             ADC_VoltageVal;
extern rt_int32_t              DS18B20_Temp;
#define GET_TEMP_CMD          "what's the temp"
#define SAMPLE_UART_NAME      "uart1"
#define DATA_CMD_END          '\r'       /* 结束位设置为 \r，即回车符 */
#define ONE_DATA_MAXLEN        20        /* 不定长数据的最大长度          */
/* 数据解析线程 */
static void serial_thread_entry(void *parameter)
{
    int8_t ch;
    int8_t data[ONE_DATA_MAXLEN];
    static int8_t i = 0;
    int8_t buf[6];
    int8_t Hundred = 0 ,Dacade = 0,Unit = 0;
    while (1)
    {
        ch = uart_sample_get_char();

        if(ch == DATA_CMD_END)
        {
            data[i++] = '\0';
            rt_kprintf("data=%s\r\n",data);
            if(0 == strcmp(data,GET_TEMP_CMD))
            {

                if(DS18B20_Temp > 0)//温度为零上
                {
                    //先打印温度
                    rt_kprintf("The current temperature is:%3d.%dC\n",
                                                DS18B20_Temp / 10,
                                                DS18B20_Temp % 10);
                    //串口输出温度
                    Hundred = (int8_t)(DS18B20_Temp / 100);//百位
                    Dacade  = (int8_t)((DS18B20_Temp - Hundred*100) / 10);//十位
                    Unit    = (int8_t)(((DS18B20_Temp - Hundred*100) - Dacade*10));//个位
                    buf[0] = Int2Char(Hundred);
                    buf[1] = Int2Char(Dacade);
                    buf[2] = '.';
                    buf[3] = Int2Char(Unit);
                    buf[4] = '\n';
                    buf[5] = Int2Char(ADC_VoltageVal/100);
                    buf[6] = Int2Char((ADC_VoltageVal%100)/10);
                    buf[7] = '\r';
                    buf[8] = '\n';
                    rt_device_write(serial, 0, buf, 9);
                }
                else
                {
                    //先打印温度
                    rt_kprintf("The current temperature is:-%2d.%dC\n",
                                                -DS18B20_Temp / 10,
                                                -DS18B20_Temp % 10);
                    //串口输出温度
                    Hundred = (int8_t)(-DS18B20_Temp / 100);//百位
                    Dacade  = (int8_t)((-DS18B20_Temp - Hundred*100) / 10);//十位
                    Unit    = (int8_t)(((-DS18B20_Temp - Hundred*100) - Dacade*10));//个位
                    buf[0]  = '-';
                    buf[1]  = Int2Char(Hundred);
                    buf[2]  = Int2Char(Dacade);
                    buf[3]  = '.';
                    buf[4]  = Int2Char(Unit);
                    buf[5] = Int2Char(ADC_VoltageVal/100);
                    buf[6] = Int2Char((ADC_VoltageVal%100)/10);
                    buf[7]  = '\r';
                    buf[8] = '\n';
                    rt_device_write(serial, 0, buf, 9);
                }
            }
            i = 0;
            continue;
        }
        i = (i >= ONE_DATA_MAXLEN-1) ? ONE_DATA_MAXLEN-1 : i;
        data[i++] = ch;
    }
}
/**
  * @brief  thread_serial
  * @param  None
  * @retval ret
  */
int thread_serial(void)
{
    rt_err_t ret = RT_EOK;
    char uart_name[RT_NAME_MAX];
    char str[] = "hello RT-Thread!\r\n";

    rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);

    /* 查找系统中的串口设备 */
    serial = rt_device_find(uart_name);
    if (!serial)
    {
        rt_kprintf("find %s failed!\n", uart_name);
        return RT_ERROR;
    }
    /* 修改串口配置参数 */
    config.baud_rate = BAUD_RATE_9600;        //修改波特率为 9600
    config.data_bits = DATA_BITS_8;           //数据位 8
    config.stop_bits = STOP_BITS_1;           //停止位 1
    config.bufsz     = 64;                   //修改缓冲区 buff size 为 128
    config.parity    = PARITY_NONE;           //无奇偶校验位

    /* 控制串口设备。通过控制接口传入命令控制字，与控制参数 */
    rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);

    /* 初始化信号量 */
    rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
    /* 以中断接收及轮询发送模式打开串口设备 */
    rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
    /* 设置接收回调函数 */
    rt_device_set_rx_indicate(serial, uart_input);
    /* 发送字符串 */
    rt_device_write(serial, 0, str, (sizeof(str) - 1));

    /* 创建 serial 线程 */
    rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10);
    /* 创建成功则启动线程 */
    if (thread != RT_NULL)
    {
        rt_thread_startup(thread);
    }
    else
    {
        ret = RT_ERROR;
    }

    return ret;
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(thread_serial, uart device sample);
INIT_APP_EXPORT(thread_serial);


